export module FileTest;
import std;
import IIS_push_str;
import "IIS_push_str.hpp";
import JsonAxios;

std::string notWebKitFormBoundary(const std::string& req_body) {
	std::regex beginReg(R"(^------WebKitFormBoundary[a-zA-Z0-9]{16}\r\n)");
	if (std::regex_search(req_body, beginReg)) {
		std::cout << "Ok: WebKitFormBoundary" << std::endl;
		std::string endStr = req_body.substr(0, 40);
		std::cout << endStr << std::endl;
		std::regex endReg(endStr + "--\r\n$");
		if (std::regex_search(req_body, endReg)) {
			std::cout << "Ok: WebKitFormBoundary End" << std::endl;
			size_t w = req_body.find("\r\n\r\n");
			if (w == std::string::npos) {
				std::cout << "No Find \\r\\n\\r\\n" << std::endl;
			}
			else {
				return req_body.substr(w + 4, req_body.size() - w - 50);
			}
		}
		else {
			std::cout << "Not: WebKitFormBoundary End" << std::endl;
		}
	}
	else {
		std::cout << "No WebKitFormBoundary found." << std::endl;
	}
	return req_body;
}

PostMapper("/filetest") {
	std::cout << "Post: /filetest" << std::endl;
	std::cout << req_body << std::endl;
	//std::cout << std::format("file:{}:end\n", notWebKitFormBoundary(req_body));
	std::ofstream ofs("filetest.png", std::ios::binary);
	ofs << notWebKitFormBoundary(req_body);
	return JsonAxios::res_200_null;
}
